<template>
  <!-- 阻止右键默认行为 -->
  <div id="app" @contextmenu.prevent.capture>
    <router-view />
  </div>
</template>

<script>
import { ipcApiRoute } from "@/api/main";
export default {
  name: "App",
  metaInfo() {
    return {
      title:
        this.$store.state.settings.dynamicTitle &&
        this.$store.state.settings.title,
      titleTemplate: (title) => {
        return title
          ? `${title} - ${process.env.VUE_APP_TITLE}`
          : process.env.VUE_APP_TITLE;
      },
    };
  },

  created() {
    // this.initWebSocket();
    // this.init()
    // console.log(this.$socketPublic)
  },
  watch: {
    "$socketPublic.getters.socketMsgs": {
      //处理接收到的消息
      handler: function (val) {
        let that = this;
        that.toDoSocket(val.data);
      },
    },
  },
  methods: {
    initWebSocket() {
      this.$socketPublic.dispatch("webSocketInit"); //初始化ws
      // console.log(this.socketMsgs)
    },
    toDoSocket: function (data) {
      data = JSON.parse(data);
      if (data.command === 12) {
        this.sendNotification({
          type: "main",
          title: "系统通知",
          subtitle: "", // macOS系统专有属性
          body: data.data,
          silent: true,
          // icon: '/asset/images/tray_logo.png',
        });
      }
    },
    init() {
      const self = this;
      // 避免重复监听，或者将 on 功能写到一个统一的地方，只加载一次
      this.$ipc.removeAllListeners(ipcApiRoute.sendNotification);
      this.$ipc.on(ipcApiRoute.sendNotification, (event, result) => {
        if (Object.prototype.toString.call(result) == "[object Object]") {
          self.$message.info(result.msg);
        }
      });
    },
    sendNotification(val) {
      console.log("val", val);
      if (this.$ipc) {
        this.$ipc.send(ipcApiRoute.sendNotification, val);
      }
    },
  },
};
</script>
